<cfif Not IsDefined("session.id")>
		<cflocation url = "/ecada/" addToken = "no">
</cfif>
	
<cfif IsDefined("session.experiment")>
	<cfset experiment = session.experiment>
<cfelse>
	<cflocation url="/ecada/" addToken = "no">
</cfif>

<cfif Not IsDefined("EditID") AND Not IsDefined("StimuliID")>
		<cflocation url="./../" addToken="no">
</cfif>
<cfparam name="StimuliID" default="">
<cfparam name="EditID" default="#StimuliID#">

<cfif IsDefined("form.submit")>
			<cfif IsDefined("form.ImageFile")>
			
				<cfset folderUID = CreateUUID()>

				<cfdirectory action = "create" directory = "/var/www/html/ecada/files/#folderUID#" mode = 777 >

				<cffile action="UPLOAD" 
					destination="#ExpandPath('/ecada/files/' & folderUID)#" 
					nameconflict="MAKEUNIQUE" 
					filefield="ImageFile"
					result="ImageDetails">

				<cfquery datasource="exp" result="filestore">
					INSERT INTO filestore (filename, filepath, fileext, mimetype)
					VALUES ('#ImageDetails.ServerFile#', '#folderUID#', '#ImageDetails.ServerFileExt#', '')
				</cfquery>
	
				<cfquery datasource="exp" result="Stimuli">
					UPDATE stim
					SET fileID = #filestore.GENERATED_KEY#,
						 Title = '#Title#'
					WHERE id = #EditID#
				</cfquery>
			<cfelse>
				<cfquery datasource="exp" result="Stimuli">
					UPDATE stim 
					set Title ='#Title#'
					WHERE id = #EditID#
				</cfquery>
			</cfif>

	<cflocation url="./../" addToken="no">
<cfelse>
	<cfinclude template= "/ecada/header.cfm">
	<cfoutput>
	<script>
				
		function test()
		{
			if($("##ImageFile").exists())
			{
				return true;
			}
			else
			{
				$("##ImageFile").remove();
				return true;
			}
			return false;
			
				if($("##ethicsFile").exists() && $("##ethicsFile").val().length > 0)
				{
					//alert("ethics exists with a length of: " + $("##ethicsFile").val().length);
					//'sal good!
				}
				else
				{
					$("##ethicsFile").remove();
				}
		}
	</script>
		<form method="post" enctype="multipart/form-data" action="./" onSubmit="test();">
			<cfquery datasource="exp" name="StimuliDetails">
				SELECT *
				FROM stim
				WHERE id = #EditID#
			</cfquery>
			<fieldset>
				<legend>
					Stimuli Options
				</legend>
				<table>
					<tr>
						<td>
							Title
						</td>
						<td>
							<input type="Textbox" value="#HTMLEditFormat(StimuliDetails.Title)#" name="Title" id="Title">
						</td>
					</tr>
<!--			<tr>
				<td>
							Description
						</td>
						<td>
							<input type="Textbox" value="#HTMLEditFormat(StimuliDetails.Description)#" name="Description" id="Description">
						</td>
					</tr>--->
				</table>
			</fieldset>
			<fieldset>
				<legend>
					Stimuli Selection
				</legend>
				<p>
					The following is your current Stimuli, click on it to view it bigger.
				</p>
				<cfquery datasource="exp" name="ImageDetails">
					SELECT *
					FROM filestore
					WHERE id = #StimuliDetails.FileID#
				</cfquery>
				<img src="/ecada/files/#ImageDetails.filepath#/#ImageDetails.filename#" width="500px" height="475px">
				<p>				
					You can select an image here to upload. The best formats for image are as follows:
					
						<ul>
							<li>PNG</li>
							<li>JPG/JPEG</li>
							<li>GIF</li>
						</ul>	
					BMP's are also acceptable, however they are usally very large in size and are not recomened. If you use BMP's please take note that they time they take to download to the participants computer will affect the timing results.
				</p>
				<input type="file" name="ImageFile" id="ImageFile">
			</fieldset>
			<input type="hidden" name="StimuliID" id="StimuliID" value="#HTMLEditFormat(EditID)#">
			<input type="submit" name="submit" id="submit">
		</form>
		<form method="post" action="./../">
			<input type="submit" value="Back">
		</form>
	</cfoutput>
	<cfinclude template= "/ecada/footer.cfm">
</cfif>



